/**
 *
 * $Id: validate.js 220 2009-09-16 09:24:49Z sunxy $
 */

var Employee = Class.create();

//defining the rest of the class implementation
Employee.prototype = {

   initialize: function(message) {
		this.message = message;
   },

   showMessage: function() {
      alert(this.message);
   },
   
   updatePassword: function(oForm) {
				
		if ($("password").value == "") {
			alert("请输入旧密码！");
			$("password").focus();
			return false;
		}
	
		if ($("newpassword").value == "") {
			alert("请输入新密码！");
			$("newpassword").focus();
			return false;
		}
	
		if ($("renewpassword").value == "") {
			alert("请输入新密码！");
			$("renewpassword").focus();
			return false;
		}
	
		if ($("renewpassword").value != $("renewpassword").value) {
			alert("新密码输入有问题！");
			$("renewpassword").focus();
			return false;
		}
		
		var sBody = getRequestBody(oForm);
	
	    var myAjax = new Ajax.Request(
	        oForm.action,
			{
			    method: 'post',
				parameters: sBody,
				onSuccess: function(request) {
					$('placeholder').innerHTML = request.responseText;
				},
				onFailure: function(request) {
					$('placeholder').innerHTML = 'error.';
				}
			}
	    );
	
		return true;
	},
	
	updateProfile: function(oForm) {
				
		var sBody = getRequestBody(oForm);
	
	    var myAjax = new Ajax.Request(
	        oForm.action,
			{
			    method: 'post',
				parameters: sBody,
				onSuccess: function(request) {
					$('placeholder').innerHTML = request.responseText;
				},
				onFailure: function(request) {
					$('placeholder').innerHTML = 'error.';
				}
			}
	    );
	
		return true;
	}
};	

//now, let's instantiate and use one object
var emp = new Employee('hi there.');
// emp.showMessage(); //displays alert

Common = {
	url: {
		domain  : 'tecmold.com',
		www		: 'http://www.tecmold.com',
		js		: 'http://js.tecmold.com',
		ns		: 'http://ns.tecmold.com',
	},
	blogtype:{blog:0, video:1, song:2, photo:3, article:4, fav:5 },
	anyOneComment: true,
	anyOneGrade: true,
	delayComment: true,
	LOGIN_BUG: null,
	is_opera: Prototype.Browser.Opera,
	is_ie: Prototype.Browser.IE,
	is_ff: Prototype.Browser.Gecko,
	noFlashPlug: function(id){
		var msg = '你还没有安装播放器或是没有开启Active X,'
				+ '<a href="'+ Common.url.www+'/help/getplayer.html" target="_blank" class="a_red">如何开启?</a>'
				+ ' [<a href="'+ Common.url.www+'/help/getplayer.html" target="_blank" class="a_red">安装播放器</a>]'
		$(id).innerHTML = msg;
	},
	getUserHead: function(uid){
		return Common.url.img2 + '/face/'+ (parseInt(uid) % 32) + '/'+ (parseInt(uid) % 257) + '/'+ uid + '/face_photo.jpg';
	},
	isLogin: function(){
		var c = '';
		for (var v in Common.cookie.names){
			eval('c = Common.cookie.names.'+v+';');
			var vv = Common.cookie.get(c);
			if (typeof(vv) != 'string' || isNaN(vv) || vv == '' ) {
				return false;
			}else{
				if(v == 'id' && parseInt(vv) < 10000){
					return false;
				}else if(v == 'name' && !Common.detect.limitLen(vv, 4, 16)){
					return false;
				}else if(v == 'sex' && parseInt(vv) < 4){
					return false;
				}else{
					return true;
				}
			}
		}
		return true;
	},
	//防止MYSQL阻塞引起登录回调停滞
	preventSlow: function(){
		Common.LOGIN_BUG = window.setInterval('Common.refreshTopMenu()',1000);
	},
	//停止 MYSQL防阻塞程序
	stopPrevent: function(flg){
		window.clearInterval(Common.LOGIN_BUG);
		typeof(_selOfLoginWindow) == 'object' && flg ? _selOfLoginWindow.close() : '';
	},
	refreshTopMenu: function(){
		
		var n = Common.cookie.names;
		var htmls = '';
		if (Common.isLogin()) {
			Common.LOGIN_BUG ? Common.stopPrevent(true) : '';
			htmls += '欢迎您！<span class="sb">'+ Common.cookie.get(n.name) +'</span>'
				   + '&nbsp;&nbsp;<span id="userNewMsg"></span>'
				   + '&nbsp;&nbsp;<a href="'+ Common.url.blog +'/'+ Common.cookie.get(n.id) +'" target="_blank">[我的主页]</a> '
				   + '&nbsp;&nbsp;<a href="'+ Common.url.my +'" target="_blank">[管理中心]</a> '
				   + '&nbsp;&nbsp;<a href="javascript:;" onclick="VLogin.logout();">[退出]</a> ';
			var u = arguments[0] ? arguments[0] : Common.url.www;
			new CheckNewMsg(u);
		} else {
			htmls += '<a href="javascript:;" onclick="VLogin.show();">[登录]</a>'
				   //+ '&nbsp;&nbsp;<a href="'+Common.ttt+'">[普通登录]</a>'
				   + '&nbsp;&nbsp;<a href="' + Common.url.passport + '/?com=register" target="_blank">[免费注册]</a>';
		}
		$('top_userPanel').innerHTML = htmls;
	},
	move: function(consult, target){
		var patchLeft = arguments[2] ? parseInt(arguments[2]) : 0;
		var patchTop = arguments[3] ? parseInt(arguments[3]) : 0;
		var p = Position.cumulativeOffset(consult);
		var l = (p[0] + parseInt(patchLeft));
		var t = (p[1] + parseInt(patchTop));
		Element.setStyle(target,{'left':l,'top':t});
	},
	copyCode: function(inputId){
		typeof(lastCopyInputId) == 'undefined' ? lastCopyInputId = inputId : '';
		$('share_'+lastCopyInputId) ? $('share_'+lastCopyInputId).innerHTML = '' : '';
		lastCopyInputId = inputId;

		var _o = $(inputId);
		_o.select();
		if(Common.is_ie) {
			clipboardData.setData('Text',_o.value);
			$('share_'+lastCopyInputId) ? $('share_'+inputId).innerHTML = "[地址已复制]" : '';
		} else {
			prompt("请复制网站地址:",_o.value);
			$('share_'+lastCopyInputId) ? $('share_'+inputId).innerHTML = "[地址已复制]" : '';
		}
	},
	copyStr: function(str){
		if(Common.is_ie) {
			clipboardData.setData('Text',str);
			alert ("“"+str+"”\n已经复制到您的剪贴板中\n您可以使用Ctrl+V快捷键粘贴到需要的地方");
		} else {
			prompt("请复制网站地址:",str);
		}
	},
	copyShareLink: function(str,type){
		if(Common.is_ie){
			clipboardData.setData('Text',str);
			if(type=='baidu'){
				confirm('代码已经复制到您的剪贴板中,是否继续查看使用演示教程?\r\n点"确定"查看教程,点"关闭"不查看进程') ? window.open('http://www.baidu.com/search/post_img.html#n1','分享代码使用教程') : '';
			}else{
				confirm('代码已经复制到您的剪贴板中,是否继续查看使用演示教程?\r\n点"确定"查看教程,点"关闭"不查看进程') ? window.open(Common.url.www+'/help/teach_share.html#'+type,'分享代码使用教程') : '';
			}
		}else{
			if(type=='baidu'){
				prompt("请复制以下代码:",str) ? window.open('http://www.baidu.com/search/post_img.html#n1','分享代码使用教程') : '';
			}else{
				prompt("请复制以下代码:",str) ? window.open(Common.url.www+'/help/teach_share.html#'+type,'分享代码使用教程') : '';
			}
		}
	},
	setErrorPic: function(obj, type){
		obj.onerror='';
		switch (type) {
		case "mv":
			obj.src = Common.url.img + "/common/default_mv.gif";
			break;
		case "photo":
			obj.src = Common.url.img + "/common/default_photo.gif";
			break;
		case "man":
			obj.src = Common.url.img + "/common/default_man.gif";
			break;
		case "sort":
			obj.src = Common.url.img + "/common/default_mv.gif";
			break;
		case "head":
			obj.src = Common.url.img + "/common/default_head.gif";
			break;
		case "face":
			obj.src = Common.url.img + "/common/default_man.gif";
			break;
		case "blank":
			obj.src = Common.url.img + "/common/blank.gif";
			break;
		}

	},
	setStartPage: function(ev){
		try{
			ev.style.behavior='url(#default#homepage)';
			ev.setHomePage(Common.url.www);
		}catch(e){
			alert('对不起,Firefox下不支持此功能,请手动设置为首页!');
		}
	},
	addFavor: function(){
			if(Common.is_ie) {
				window.external.addFavorite(Common.url.www,'酷6网');
			} else if (Common.is_opera) {
				alert ("请使用Ctrl+T将本页加入收藏夹");
			} else {
				alert ("请使用Ctrl+D将本页加入收藏夹");
			}
	},
	removeUBB: function(){
	},
	openItem: function(id){
		try {
			$(id).toggle();
		} catch (e)	{}
	},
	stopBubble: function(ev,callFunc){
		try{
			ev.cancelBubble = true;
		}catch (ex){
			ev.stopPropagation();
		}{typeof(callFunc) != 'undefined' ? callFunc() : '';}
	},
	switchTab: function (tabpage, tabid, showid){
		var oItem = $(tabpage).getElementsByTagName("li");
		var count_oItem = oItem.length;
		for(var i=0; i<count_oItem; i++){
			var x = oItem[i];
			x.className = "";
			var y = x.getElementsByTagName('a');
		}
		$(tabid).className = "Selected";
		var dvs = $(showid).getElementsByTagName("div");
		var count_dvs = dvs.length;
		for (var i=0; i<count_dvs; i++){
			if (dvs[i].id==('d'+tabid)){
				dvs[i].show();
			}else{
				dvs[i].hide();
			}
		}
	},
	switchAjaxTab: function (did,sid,userAjaxFunc){
		eval(userAjaxFunc+"();");
		var os = $(did).getElementsByTagName("li");
		var c = os.length;
		var i = 0;
		while(i < c){
			os[i].className = '';
			i++;
		}
		$(sid).className = "Selected";
	},
	toolTip: {
		sPop: null,
		popStyle: null,
		showPopupText: function(event) {
			if(event.srcElement) o = event.srcElement; else o = event.target;
			if(!o) {
				return;
			}
			MouseX = event.clientX;
			MouseY = event.clientY;
			if(o.alt != null && o.alt != '') {
				o.pop = o.alt;
				o.alt = '';
			}
			if(o.title != null && o.title != '') {
				o.pop = o.title;
				o.title = '';
			}
			if(o.pop != Common.toolTip.sPop) {
				Common.toolTip.sPop = o.pop;
				if(Common.toolTip.sPop == null || Common.toolTip.sPop == '') {
					$('popLayer').style.visibility = "hidden";
				} else {
					Common.toolTip.popStyle = o.dyclass != null ? o.dyclass : 'cPopText';
					$('popLayer').style.visibility = "visible";
					Common.toolTip.showIt();
				}
			}
		},
		showIt: function() {
			var _o = $('popLayer');
			_o.className = Common.toolTip.popStyle;
			_o.innerHTML = Common.toolTip.sPop.replace(/<(.*)>/g,"&lt;$1&gt;").replace(/\n/g,"<br>");
			var popWidth = _o.clientWidth;
			var popHeight = _o.clientHeight;
			var popLeftAdjust = MouseX + 12 + popWidth > document.body.clientWidth ? -popWidth - 24 : 0;
			var popTopAdjust = MouseY + 12 + popHeight > document.body.clientHeight ? -popHeight - 24 : 0;
			_o.style.left = (MouseX + 12 + document.body.scrollLeft + popLeftAdjust) + 'px';
			_o.style.top = (MouseY + 12 + document.body.scrollTop + popTopAdjust) + 'px';
		}
	},
	dom: {
		getNodeValue: function(node, path){
			if (Common.is_ie) {
				return node.selectSingleNode(path).text;
			} else {
				return node.getElementsByTagName(path)[0].textContent;
			}
		},
		getNodes: function(node,path){
			//if (Common.is_ie) {
				//return node.selectNodes(path);
			//} else {
				return node.getElementsByTagName(path);
			//}
		}
	},
	cookie: {
		path  : '/',
		domain: 'pica.com',
		names: {
			id	  : 'sysUserId',
			name  : 'sysUserName',
			sex	  : 'sysUserSex',
			secure: 'sysUserSecure',
			state : 'sysUserState'
		},
		get: function(n){
			var oc = document.cookie;
			var v = "";
			var s = n + "=";
			var os= -1;
			var end;
			if (oc.length > 0) {
				os = oc.indexOf(s);
				if (os != -1) {
					os += s.length;
					end = oc.indexOf(";", os);
					
					if (end == -1){
						end = oc.length;
					}
					v = unescape(decodeURI(oc.substring(os,end)));
				}
			}
			return v;
		},
		set: function(n,value,seconds){
			var argv = arguments;
			var argc = arguments.length;
			var expires = (2 < argc) ? (new Date((new Date()).getTime() + argv[2]*1000)) : null;
			var path	= (3 < argc) ? argv[3] : Common.cookie.path;
			var domain	= (4 < argc) ? argv[4] : Common.cookie.domain;
			var secure	= (5 < argc) ? argv[5] : false;
			
			var cs  = n + "=" + encodeURI(value)
					+ ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
					+ ((path == null) ? "" : ("; path=" + path))
					+ ((domain == null) ? "" : ("; domain=" + domain))
					+ ((secure == true) ? "; secure" : "");
			document.cookie = cs;
		},
		del: function(n){
			var argv = arguments;
			var argc = arguments.length;
			var exp  = new Date((new Date()).getTime() - 3600);
			var path = (argc > 1) ? argv[1] : Common.cookie.path;
			var domain = (argc > 2) ? argv[2] : Common.cookie.domain;
			var secure = (argc > 3) ? argv[3] : false;
			switch(argc){
				case 1:
					Common.cookie.set(n,'',exp);
					return true;
					break;
				case 2:
					Common.cookie.set(n,'',exp, path);
					return true;
					break;
				case 3:
					Common.cookie.set(n,'',exp, path, domain);
					return true;
					break;
				default:
					alert('Cookie["'+n+'"] Can\'t Delete.');
					return false;
					break;
			}
		}
	},
	ajax: {
		loadData: function(u,p,l,s, f,r){
			if(arguments.length < 4){
				return ;
			}
			if (r) {
				p.get._rand = Math.random();
			}
			var url = u;
			var _getPar = p.get ? $H(p.get).toQueryString() : '';
			if(_getPar != ''){
				url += '?' + _getPar;
			}
			new Ajax.Request(
				url,
				{
				  method: 'post',
				  parameters: p.post ? $H(p.post).toQueryString() : '',
				  onLoading: l,
				  onSuccess: s,
				  onFailure: f
				}
			);
		},
		loadElement: function(e,u,p, f,l,r){
			if(arguments.length < 3){
				return ;
			}
			if (r) {
				p._rand = Math.random();
			}
			var url = u;
			var _getPar = p.get ? $H(p.get).toQueryString() : '';
			if(_getPar != ''){
				url += '?' + _getPar;
			}
			Element.update(e, l);
			new Ajax.Updater(
				{success: e},
				url,
				{
				  method: 'post',
				  parameters: p.post ? $H(p.post).toQueryString() : '',
				  onFailure: f
				}
			);
		}
	},
	dispose: {
		trim: function(s){
			return s.replace(/^\s*|\s*$/g,'');
		},
		trimBlankLine: function(s){
			return s.replace(/(\n[\s|\t]*\r*\n)/g, '\n');
		},
		ltrim:function(s){
 			return s.replace(/(^\s*)/g,"");
		},
		rtrim:function(s){
 			return s.replace(/(\s*$)/g,"");
		},
		delClipBoardCN: function(){
			var str = window.clipboardData.getData("Text");
			var s = str.replace(/[^\x00-\xff]/g,'');
			window.clipboardData.setData("Text",s);
		},
		disableCN: function(){
			var o;
			for (var i = 0; i < arguments.length; i++) {
				o = arguments[i];
				if (typeof(o) == 'string'){ o = $(o); }
				Element.setStyle(o,{imeMode:"disabled"});
				Event.observe( o, "beforepaste", Common.dispose.delClipBoardCN, false );
			}
		},
		transHTML: function(s){
			//s = s.replace(/&/g,"&amp;");
			//s = s.replace(/</g,"&#60;");
			//s = s.replace(/>/g,"&#62;");
			//s = s.replace(/ /g,"&nbsp;");
			//s = s.replace(/\n/g,"<br />");
			return s;
		},
		htmlDecode: function(s){
			if(s == null){return s;}
			return s.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&");
		},
		HtmlEncode: function(s){
			if(s == null){return s;}
			return s.replace(/</g,"&lt;").replace(/>/g,"&gt;");
			//	return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		},
		replaceAll: function(Str, Src, NewStr){
			if(!Str || Str.length == 0) return "";
			return Str.split(Src).join(NewStr);
			var Buf = Str;
			if (Src.length == 0) return Buf;
			var Idx = Buf.indexOf(Src);
			while (Idx >= 0){
				Buf = Buf.substring(0, Idx) + NewStr + Buf.substr(Idx + Src.length);
				Idx = Buf.indexOf(Src, Idx + NewStr.length);
			}
			return Buf;
		},
		getLength: function(str){
			var templen = str.length;
			//if(navigator.appName == 'Netscape'){return templen;}
			for (var i = 0; i < str.length; i++) {
				var rstr = escape(str.substring(i,i+1));
				if (rstr.substring(0,2) == "%u"){ templen++; }
			}
			return templen;
		},
		sub_str: function(str,len,place){
			if(str.replace(/[^\x00-\xff]/gi,'xx').length <= len) return str;
			if(place=='left'){
				var temp_len=len;
				str=str.substr(0,len);
				while(str.replace(/[^\x00-\xff]/gi,'xx').length > len){
					str=str.substr(0,--temp_len);
				}
			}
			if(place=='right'){
				var temp_startn=0;
				str=str.substr(str.length-len,len);
				while(str.replace(/[^\x00-\xff]/gi,'xx').length>len){
					str=str.substr(1,len);
				}
			}
			return str;
		}
	},
	detect: {
		isEmpty: function(s){
			return Common.dispose.trim(s).length == 0;
		},
		isNumber: function(s){
			return !isNaN(s);
		},
		isDouble: function(s){
			return (/^[-\+]?\d+(\.\d+)?$/).test(Common.dispose.trim(s));
		},
		isInteger: function(s){
			return (/^[-\+]?\d+$/).test(Common.dispose.trim(s));
		},
		isEnglish: function(s){
			return (/^[A-Za-z]+$/).test(Common.dispose.trim(s));
		},
		isChinese: function(s){
			return (/^[\u0391-\uFFE5]+$/).test(Common.dispose.trim(s));
		},
		isDoubleChar: function(s){
			return (/^[^\x00-\xff]+$/).test(Common.dispose.trim(s));
		},
		isDate: function(s){
			return (/^\d{4}|\d{2}-\d{1,2}-\d{1,2}$/).test(Common.dispose.trim(s));
		},
		isQQ: function(s){
			return (/^[1-9]\d{4,11}$/).test(Common.dispose.trim(s));
		},
		isEmail: function(s){
			return (/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/).test( Common.dispose.trim(s) );
		},
		isMobile: function(s){
			return (/^1[3|5]\d{9}$/).test(Common.dispose.trim(s));
		},
		isPhone: function(s){
			return (/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/).test(Common.dispose.trim(s));
		},
		isZip: function(s){
			return (/^[1-9]\d{5}$/).test(Common.dispose.trim(s));
		},
		isCard: function(s){
			var aCity="11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,";
			var iSum=0;
			var info="";
			var sId="";
			if (s.length==15){
				sId=s.substr(0,6)+"19"+s.substr(6,9)+"0";
			}else if (s.length==18){
				sId=s;
			}else{
				return false;
			}
	
			xb=Number(sId.substr(16,1))%2;
	
			if(!/^\d{17}(\d|x)$/i.test(sId)){return false;}
			sId=sId.replace(/x$/i,"a");
			if(aCity.indexOf(sId.substr(0,2))==-1){return false;}
			sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
			var d=new Date(sBirthday.replace(/-/g,"/"));
			if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){
				return false;
			}
			if (s.length==18) {
				for(var i = 17;i>=0;i --){
					iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
				}
				if(iSum%11!=1) {return false;}
			}
			return true;
		},
		isURL: function(s){
			return (/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/).test(Common.dispose.trim(s).toLowerCase());
		},
		isTag: function(s){
			return !(/[-\~!@#\$%\^&\*\,\.\(\)\[\]\{\}<>\?\\\/\'\"\|《》\、\，\。\‘\’\“\”\？\￥\……\！]/i).test(s);
		},
		isColor: function(s){
			if (Common.dispose.trim(s).length != 7) return false;
			return Common.dispose.trim(s).search(/\#[a-fA-F0-9]{6}/) != -1;
		},
		isRubbish: function(s){
			return (/[\w-\~!@#\$%\^&\*\,\.\(\)\[\]\{\}<>\?\\\/\'\"\|]{55,}/i).test(s)
		},
		hasInvalidateVCodeChar: function(s){
			return (/[^a-zA-Z0-9]+$/).test(s);
		},
		hasChineseChar: function(s){
			return (/[^\x00-\xff]/).test(s);
		},
		hasAvailableChar: function(s){
			return (/^[a-zA-Z0-9_-]{0,}$/).test(s);
		},
		hasUnAvailableChar: function(s){
			return (/[^a-zA-Z0-9_-]/i).test(Common.dispose.trim(s).toLowerCase());
		},
		hasUnAvailableChar2: function(s){
			return (/[^\u0391-\uFFE5a-zA-Z0-9_-]/i).test(Common.dispose.trim(s).toLowerCase());
		},
		limitLen: function(s,Min,Max){
			s = Common.dispose.trim(s);
			var ss = Common.dispose.getLength(s);
			if( ss < Min || ss > Max ){
				return false;
			}else{
				return true;
			}
		}
	}
};
